/**
 * 11选5的共用逻辑
 */

// 标题
initGame(game, function() {
    // 填充tab页
    fill_tabs(['两面', '第一球', '第二球', '第三球', '第四球', '第五球', '连码']);

    fill_table();
}, true);

function fill_table() {
    var types = ["大", '小', "单", '双', "尾大", '尾小', "龙", '虎'];
    var div = $(".zonghe");
    div.empty();
    for (var i = 0; i < types.length; i++) {
        var key = types[i];
        var name = types[i];
        if (key == '大' || key == '小' || key == '单' || key == '双') {
            key = '和' + key;
            name = '总和' + name;
        }
        if (key == '尾大' || key == '尾小') name = '总和' + name;
        add_bet_item(key, name, div);
    }

    var arr = ['一', '二', '三', '四', '五'];
    var types = ["大", "小", "单", "双"]; 
    var div = $(".five");
    div.empty();
    for (var i in arr) {
        add_bet_th("第" + arr[i] + "球", div);
        var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
        for (var j = 0; j < types.length; j++) {
            add_bet_item(arr[i] + types[j], types[j], ul);
        }
        div.append(ul);
    }

    for (var i = 0; i < arr.length; i++) {
        var div = $(".q" + (i + 1));
        div.empty();

        // 1-9
        var ul = $('<ul class="am-avg-sm-3 bet"></ul>');
        for (var j = 1; j <= 9; j++) {
            var k = j < 10 ? "0" + j : j;
            add_bet_item(arr[i] + k, '<img src="../public/all/images/' + k + '.png">', ul);
        }
        div.append(ul);

        var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
        add_bet_item(arr[i] + "10", '<img src="../public/all/images/10.png">', ul);
        add_bet_item(arr[i] + "11", '<img src="../public/all/images/11.png">', ul);
        for (var j = 0; j < types.length; j++) {
            add_bet_item(arr[i] + types[j], types[j], ul);
        }
        div.append(ul);
    }
}

window.lastest_result2 = function() {
    var c = Result.last();
    if (!c || !c.red) return [];
    var red = c.red;
    var r = [];

    // 冠亚军和
    var he = gameRule.he(red);
    r.push(he);

    if (gameRule.isHeBig(red)) r.push("大");
    else if (gameRule.isHeSmall(red)) r.push("小");

    if (gameRule.isHeOdd(red)) r.push("单");
    else r.push("双");

    if (gameRule.isHeWeiBig(red)) r.push("尾大");
    else r.push("尾小");

    if (gameRule.isLong(red))
        r.push("龙");
    else if (gameRule.isHu(red))
        r.push("虎");
    else if (gameRule.isHe(red))
        r.push("和");

    return r;
}

// 检测能否下注，看下单号1-10是否满足要求
window.customCheckBetList = function(type, issue, data) {
    if (type != 'gd115' && type != 'zy115') return true;

    var limit = Util.getNumberLimit(type);
    if (limit <= 0 || limit >= 10) return true;

    var arr1 = ['一', '二', '三', '四', '五'];
    var arr2 = ['01', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11'];
    var m = {};
    for (var k in data) {
        if (k.length != 3) continue;
        var k1 = k[0], k2 = k.substring(1);
        if (arr1.indexOf(k1) != -1 && arr2.indexOf(k2) != -1) {
            if (!m[k1]) m[k1] = {};
            m[k1][k2] = true;
        }
    }
    if (Util.isEmpty(m)) return true;

    // 获取下已经下注的单据
    var bets = context.bets[type];
    if (bets) bets = bets[issue] || [];
    else bets = [];
    for (var i in bets) {
        var k = bets[i].target;
        if (k.length != 3) continue;
        var k1 = k[0], k2 = k.substring(1);
        if (arr1.indexOf(k1) != -1 && arr2.indexOf(k2) != -1) {
            if (!m[k1]) m[k1] = {};
            m[k1][k2] = true;
        }
    }

    var total = 0;

    for (var k in m) {
        var m2 = m[k];
        if (Object.keys(m2).length > limit) {
            var l = "下注失败，__g__ 定位玩法，单个名次不允许下注__v__码或者__v__以上！如有疑问，请咨询客服！";
            l = l.replace(/__g__/, get_game_display_name(type));
            l = l.replace(/__v__/g, limit);
            alert2(l, true);
            return false;
        }
    }

    return true;
}